remote-add: Add --force option to add or replace remote
authorDan Nicholson <nicholson@endlessm.com>
Tue, 12 Sep 2017 22:05:08 +0000 (17:05 -0500)
committerAtomic Bot <atomic-devel@projectatomic.io>
Fri, 8 Feb 2019 14:36:41 +0000 (14:36 +0000)
This uses the OSTREE_REPO_REMOTE_CHANGE_REPLACE operation to add a
remote or replace an existing one. This is roughly the opposite of
--if-not-exists and will raise an error if both options are passed.

Closes: #1166
Approved by: cgwalters

bash/ostree
man/ostree-remote.xml
src/ostree/ot-remote-builtin-add.c
tests/test-remote-add.sh

index 52b111ec577a5c5f51c4c60636f7eb3d52a82582..5ba3d4759f236f3a0bb2dabc6b2ebbbf6ad17f89 100644 (file)
@@ -985,6 +985,7 @@ _ostree_remote_add() {
     local boolean_options="
         $main_boolean_options
         --if-not-exists
+        --force
         --no-gpg-verify
     "
 
index 5e2180582b95a488b2370473a83d7f06873490c1..407f7e3d2c3ecbefa69406ce3a931666b83e57c9 100644 (file)
@@ -136,6 +136,14 @@ Boston, MA 02111-1307, USA.
                 </para></listitem>
             </varlistentry>
 
+            <varlistentry>
+                <term><option>--force</option></term>
+
+                <listitem><para>
+                    Replace the provided remote if it exists.
+                </para></listitem>
+            </varlistentry>
+
             <varlistentry>
                 <term><option>--no-gpg-verify</option></term>
 
index 8b339dbd6cd4bcc6ebc76420dae9cd7e30db74cd..f81f7580730677369fffa10c7f34ec96ebb20a18 100644 (file)
@@ -30,6 +30,7 @@
 static char **opt_set;
 static gboolean opt_no_gpg_verify;
 static gboolean opt_if_not_exists;
+static gboolean opt_force;
 static char *opt_gpg_import;
 static char *opt_contenturl;
 static char *opt_collection_id;
@@ -45,6 +46,7 @@ static GOptionEntry option_entries[] = {
   { "set", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_set, "Set config option KEY=VALUE for remote", "KEY=VALUE" },
   { "no-gpg-verify", 0, 0, G_OPTION_ARG_NONE, &opt_no_gpg_verify, "Disable GPG verification", NULL },
   { "if-not-exists", 0, 0, G_OPTION_ARG_NONE, &opt_if_not_exists, "Do nothing if the provided remote exists", NULL },
+  { "force", 0, 0, G_OPTION_ARG_NONE, &opt_force, "Replace the provided remote if it exists", NULL },
   { "gpg-import", 0, 0, G_OPTION_ARG_FILENAME, &opt_gpg_import, "Import GPG key from FILE", "FILE" },
   { "contenturl", 0, 0, G_OPTION_ARG_STRING, &opt_contenturl, "Use URL when fetching content", "URL" },
   { "collection-id", 0, 0, G_OPTION_ARG_STRING, &opt_collection_id,
@@ -84,6 +86,14 @@ ot_remote_builtin_add (int argc, char **argv, OstreeCommandInvocation *invocatio
       goto out;
     }
 
+  if (opt_if_not_exists && opt_force)
+    {
+      ot_util_usage_error (context,
+                           "Can only specify one of --if-not-exists and --force",
+                           error);
+      goto out;
+    }
+
   remote_name = argv[1];
   remote_url  = argv[2];
 
@@ -135,9 +145,14 @@ ot_remote_builtin_add (int argc, char **argv, OstreeCommandInvocation *invocatio
 
   options = g_variant_ref_sink (g_variant_builder_end (optbuilder));
 
-  if (!ostree_repo_remote_change (repo, NULL,
-                                  opt_if_not_exists ? OSTREE_REPO_REMOTE_CHANGE_ADD_IF_NOT_EXISTS : 
-                                  OSTREE_REPO_REMOTE_CHANGE_ADD,
+  OstreeRepoRemoteChange changeop;
+  if (opt_if_not_exists)
+    changeop = OSTREE_REPO_REMOTE_CHANGE_ADD_IF_NOT_EXISTS;
+  else if (opt_force)
+    changeop = OSTREE_REPO_REMOTE_CHANGE_REPLACE;
+  else
+    changeop = OSTREE_REPO_REMOTE_CHANGE_ADD;
+  if (!ostree_repo_remote_change (repo, NULL, changeop,
                                   remote_name, remote_url,
                                   options,
                                   cancellable, error))
index 9aa5c6a22d34077bb4490bee0736cd00175daaf1..57fe7eec592e76ca7df3ca007e9b8e41fa58d349 100755 (executable)
@@ -23,7 +23,7 @@ set -euo pipefail
 
 . $(dirname $0)/libtest.sh
 
-echo '1..14'
+echo '1..16'
 
 setup_test_repository "bare"
 $OSTREE remote add origin http://example.com/ostree/gnome
@@ -106,3 +106,15 @@ assert_not_file_has_content list.txt "origin"
 # Can't grep for 'another' because of 'another-noexist'
 assert_file_has_content list.txt "another-noexist"
 echo "ok remote list remaining"
+
+# Both --if-not-exists and --force cannot be used
+if $OSTREE remote add --if-not-exists --force yetanother http://yetanother.com/repo 2>/dev/null; then
+    assert_not_reached "Adding remote with --if-not-exists and --force unexpectedly succeeded"
+fi
+echo "ok remote add fail --if-not-exists and --force"
+
+# Overwrite with --force
+$OSTREE remote add --force another http://another.example.com/anotherrepo
+$OSTREE remote list --show-urls > list.txt
+assert_file_has_content list.txt "^another \+http://another.example.com/anotherrepo$"
+echo "ok remote add --force"